package com.atgg.condition;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.Map;

/**
 * 实现Condition接口
 * 当操作系统为windows时，执行
 */
public class LinuxCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {


        Map<String, Object> annotationAttributes = annotatedTypeMetadata.getAnnotationAttributes("@Bean");


        // 获取到ioc使用的beanFactory
        ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
        //  获取类加载器
        ClassLoader classLoader = conditionContext.getClassLoader();
//        System.out.println(classLoader);
        // 获取运行的环境
        Environment environment = conditionContext.getEnvironment();
        String property = environment.getProperty("os.name");
        if (property.toLowerCase().contains("linux")){
            return true;
        }
        // 获取bean定义的信息
        BeanDefinitionRegistry registry = conditionContext.getRegistry();
        return false;
    }
}
